package com.dacool.fragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView.Validator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dacool.app.sale.R;
import com.dacool.http.BaseRequest;
import com.dacool.http.BaseRequest.ResponseHandler;
import com.dacool.http.RecommendRequest;
import com.dacool.util.ViewUtils;

public class AddRecommenderFragment extends BaseFragment implements
		OnClickListener {

	private EditText nameInput;
	private EditText phoneInput;
	private Button submitButton;

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.frag_recommend_people;
	}

	@Override
	protected View myView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean showDefaultSecondLeftButton() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		nameInput = ViewUtils.findViewById(view, R.id.nameInput);
		phoneInput = ViewUtils.findViewById(view, R.id.phoneInput);
		submitButton = ViewUtils.findViewById(view, R.id.submit);
		submitButton.setOnClickListener(this);
	}

	private boolean verify() {
		if (this.nameInput.getText().length() == 0) {
			Toast.makeText(getActivity(), "请输入姓名", Toast.LENGTH_LONG).show();
			return false;
		}
		Pattern p = Pattern.compile("^1\\d{10}$");
		Matcher matcher = p.matcher(phoneInput.getText().toString());
		if (!matcher.matches()) {
			Toast.makeText(getActivity(), "请输入11位手机号", Toast.LENGTH_LONG)
					.show();
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {

		if (v == submitButton) {

			if (verify()) {
				this.showProgressDialog(null, false);
				new RecommendRequest()
						.setMobileNo(phoneInput.getText().toString())
						.setName(nameInput.getText().toString())
						.setHandler(new ResponseHandler() {

							@Override
							public void handleResponse(BaseRequest request,
									int statusCode, String errorMsg,
									String response) {
								// TODO Auto-generated method stub
								AddRecommenderFragment.this
										.dismissProgressDialog();
								if (statusCode != 200) {
									ViewUtils.runInMainThread(new Runnable() {

										@Override
										public void run() {
											Toast.makeText(getActivity(),
													"推荐出错", Toast.LENGTH_LONG)
													.show();
										}
									});
								} else {
									JSONObject obj;
									boolean success = false;
									String errmsg = null;
									try {
										obj = new JSONObject(response);
										success = obj.optBoolean("success",
												false);
										errmsg = obj.optString("msg", null);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									if (success) {
										getContainerActivity()
												.handleBackButton();
									} else {
										String msg = errmsg != null ? errmsg
												: "推荐出错";
										ViewUtils.showToast(msg,
												Toast.LENGTH_LONG);
									}

								}
							}
						}).start();
			}
		}
		super.onClick(v);
	}

	@Override
	public String titleText() {
		// TODO Auto-generated method stub
		return "我要推荐";
	}

}
